<?php

namespace App\Admin\Controllers\AppStore;

use App\Admin\Extensions\ArticleOp;
use App\Http\Controllers\Controller;
use App\Model\AppStore\Article;
use App\Util\ComUtil;
use Encore\Admin\Grid;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form\Tools;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Layout\Content;
// use Encore\Admin\Widgets\Form;
use Encore\Admin\Form;

class ArticleController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('文章管理');
        $content->body($this->grid());
        return $content;
    }

    public function edit($id)
    {
        $content = new Content();
        $content->header('文章管理');
        $content->body($this->form('edit', $id)->edit($id));
        return $content;
    }

    public function create()
    {
        $content = new Content();
        $content->header('文章管理');
        $content->body($this->form());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new Article());
        $grid->model()->orderBy('is_rec', 'desc')->orderBy('id', 'desc');
        $grid->id('文章ID');
        $grid->title('文章标题');
        $grid->sub_text('文章简介');
        $grid->category('文章类别')->display(function ($data) {
            return Article::CATE[$data];
        });
        $grid->thumb('缩略图')->display(function ($data) {
            return ComUtil::buidlImgaeHtml($data, 400, 300, true);
        });
        $grid->link('外链地址');
        $grid->is_rec('首页精选')->display(function ($data) {
            return $data == 1 ? '精选' : '-';
        });
        $grid->flag('状态')->display(function ($data) {
            return $data == 1 ? '有效' : '下架';
        });
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->like('title', '标题');
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->like('content', '正文');
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('flag', '状态')->select([0 => '下架', 1 => '上架']);
            });
        });

        $grid->actions(function (Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            // $actions->disableEdit();
            $actions->append(new ArticleOp(
                $actions->getKey(),
                $actions->row,
                'article/verify'
            ));
        });
        return $grid;
    }

    protected function form($mode = '', $id = '')
    {
        $form = new Form(new Article());
        $form->text('title', '文章标题')->required();
        $form->text('sub_text', '文章描述')->required();
        $form->image('thumb', '缩略图')->required();
        $form->text('link', '外链地址');
        // $form->editor('content', '正文');
        // $form->UEditor('content');
        // options 中参数会覆盖 extensions.ueditor.config 中参数
        $form->UEditor('content')->options(['initialFrameHeight' => 1000]);
        // $form->summernote('content', '正文');
        // $form->tinymce('content', '正文');

        $form->hidden('author');
        $form->select('category', '文章类别')->options(Article::CATE)->default(1);
        $form->select('is_rec', '是否精选')->options([1 => '精选', 0 => '非精选'])->default(0)->required();
        $form->select('flag', '是否有效')->options([1 => '上架', 0 => '下架'])->default(1)->required();
        $form->saving(function (Form $form) {
            $form->author = '';
        });
        $form->tools(function (Tools $tools) {
            $tools->disableList();
            $tools->disableDelete();
            $tools->disableView();
        });
        return $form;
    }

    function verify()
    {
        $id = request('id');
        $status = request('status');
        $record = Article::find($id);
        if (!$record) return ['ret' => -1, 'message' => '记录不存在'];
        //修改状态
        $record->update(['flag' => $status]);
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
